<?php

namespace Api\Controller;

class ClassController extends ApiController {

    private $token;
    private $deviceid;
    private $user_id;
    private $class;
    private $userinfo;
    private $error_class;

    function __construct() {
        parent::__construct();
        $this->error_class = L('error_class');
        $this->token = I('request.token');
        $this->deviceid = I('request.deviceid');
        $this->userinfo = $this->verify_token($this->token, $this->deviceid);
        $this->user_id = $this->userinfo['user_id'];
    }

    private function is_power($user_id, $class_id, $power = 10) {
        $Member = D('Member');
        $classmember = $Member->getMembyids($user_id, $class_id);
        if ($classmember['power'] >= $power) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //班级code加密函数
    private function getclasscode() {
        $char = getsalt(1);
        return substr(strtoupper(strrev(dechex(time()))), 0, 7) . $char;
    }

    public function createclass() {
        $classname = I('request.classname');
        $description = I('request.description');
        $remarks = I('request.remarks');
        $remarks = empty($remarks) ? '班主任' : $remarks;
        $school = I('request.school');
        $user = $this->is_teacher($this->user_id);
        if (!$user || empty($user) || $user['group'] != 3) {
            $this->error($this->error_class['noteacher']);
        }
        if (strlen($classname) < 8 || strlen($classname) > 20) {
            $this->error($this->error_class['longername']);
        }
        $username = $user['username'];
        $class_code = $this->getclasscode();
        $Class = D('Class');
        $class_id = $Class->addClass($classname, $description, $school, $this->user_id, $username, $class_code);
        if ($class_id) {
            $ClassMember = D('Member');
            $nickname = I('request.nickname');
            $nickname = empty($nickname) ? $user['nickname'] : $nickname;
            $ClassMember->addMember($this->user_id, $class_id, $nickname, $remarks, 10);
            $arrInfo = array(
                'classname' => $classname,
                'description' => $description,
                'class_id' => $class_id,
                'code' => $class_code,
            );
            $this->success(__FUNCTION__, $arrInfo);
        } else {
            $this->error($this->error_class['oserror']);
        }
    }

    public function upclass() {
        $iCid = I('request.classid');
        $aData['classname'] = I('request.classname');
        $aData['school'] = I('request.school');
        $aData['description'] = I('request.description');
        if (empty($iCid)) {
            $this->error($this->error_class['oserror']);
        }
        if (empty($aData['classname']) && empty($aData['school']) && empty($aData['description'])) {
            $this->error($this->error_class['noupdata']);
        }
        if (!$this->is_power($this->user_id, $iCid)) {
            $this->error($this->error_class['noadmin']);
        }
        $mClass = D('Class');
        $aClass = $mClass->getClassbycid($iCid);
        if (empty($aClass)) {
            $this->error($this->error_class['noclass']);
        }
        $aUpdata = array();
        foreach ($aData as $key => $value) {
            if ($aClass[$key] != $value) {
                $aUpdata[$key] = $value;
            }
        }
        if (empty($aUpdata)) {
            $this->error($this->error_class['noupdata']);
        }
        if ($mClass->upClassbyId($iCid, $aUpdata)) {
            $aUpdata['class_id'] = $class_id;
            $arrInfo = $aUpdata;
            $this->success(__FUNCTION__, $arrInfo);
        } else {
            $this->error($this->error_class['oserror']);
        }
    }

    public function upclasscode() {
        $class_id = I('request.classid');
        if (!$this->is_power($this->user_id, $class_id)) {
            $this->error($this->error_class['noadmin']);
        }
        $class_code = $this->getclasscode();
        $updata = array(
            'code' => $class_code
        );
        $Class = D('Class');
        if ($Class->upClass($class_id, $updata)) {
            $arrInfo = $updata;
            $this->success(__FUNCTION__, $arrInfo);
        } else {
            $this->error($this->error_class['oserror']);
        }
    }

    public function getclassinfo() {
        $class_id = I('request.classid');
        $Member = D('Member');
        $meminfo = $Member->getMembyids($this->user_id, $class_id);
        if (!$meminfo['power'] || empty($meminfo['power'])) {
            $this->error($this->error_class['noinclass']);
        }
        $Class = D('Class');
        $Post = D('Post');
        $info = $Class->getClassbycid($class_id);
        $info['number'] = $Member->getCountbycid($class_id);
        $info['postnum'] = $Post->getPostnumbycid($class_id);
        $this->success(__FUNCTION__, $info);
    }

    public function getclassmembers() {
        $iLimit = 30;
        $iCid = I('request.classid');
        $iStart = I('request.startid') != FALSE ? I('request.startid') : 0;
        $mCmember = D('Member');
        $aMeminfo = $mCmember->getMembyids($this->user_id, $iCid);
        if (!$aMeminfo['power'] || empty($aMeminfo['power'])) {
            $this->error($this->error_class['noinclass']);
        }
        $info['members'] = $mCmember->getmListbycid($iCid, $iStart, $iLimit);
        if (count($info['members']) < $iLimit) {
            $info['page'] = count($info['members']);
        } else {
            $info['page'] = $iStart * $iLimit + $iLimit;
        }
        $this->success(__FUNCTION__, $info);
    }

    public function upmemberpower() {
        $iPower = I('request.power');
        $iUid = I('request.userid');
        $iCid = I('request.classid');
        if (empty($iUid) || empty($iCid)) {
            $this->error($this->error_class['invalid']);
        }
        if (!$this->is_power($this->user_id, $iCid)) {
            $this->error($this->error_class['noadmin']);
        }
        $mCmember = D('Member');
        $aCmember = $mCmember->getMembyids($iUid, $iCid);
        if (!$aCmember['id'] || empty($aCmember['id'])) {
            $this->error($this->error_class['oserror']);
        }
        if ($aCmember['power'] == $iPower) {
            $this->error($this->error_class['ispower']);
        }
        $aUpdata = array('power' => $iPower);
        if ($mCmember->upMemberbyids($iUid, $iCid, $aUpdata)) {
            $this->warnclass($iUid, $iCid, $aCmember['power'], $iPower);
            $aCmember['power'] = $iPower;
            $info = $aCmember;
            $this->success(__FUNCTION__, $info);
        } else {
            $this->error($this->error_class['oserror']);
        }
    }

    function delmember() {
        $user_id = I('request.userid');
        $class_id = I('request.classid');
        if (empty($user_id) || empty($class_id)) {
            $this->error($this->error_class['invalid']);
        }
        if (!$this->is_power($this->user_id, $class_id)) {
            $this->error($this->error_class['noadmin']);
        }
        $Member = D('Member');
        if ($Member->delMemberbyids($user_id, $class_id)) {
            $this->warnclass($user_id, $class_id);
            $info = $this->error_class['delok'];
            $this->success(__FUNCTION__, $info);
        } else {
            $this->error($this->error_class['oserror']);
        }
    }

    protected function warnclass($iUid, $iCid, $iOpower = 0, $iNpower = 0) {
        $aClass = D('Class')->getClassbycid($iCid);
        switch ($iOpower) {
            case 0:
                if ($iNpower == 0) {
                    $sContent = '班主任拒绝你加入' . $aClass['classname'];
                    $this->jpushapi(array($iUid), $sContent, '', 3);
                } else {
                    $sContent = '班主任同意你加入' . $aClass['classname'];
                    $this->jpushapi(array($iUid), $sContent, '', 3);
                }
                break;
            case 3:
                if ($iNpower == 0) {
                    $sContent = '你已被班主任移出' . $aClass['classname'];
                    $this->jpushapi(array($iUid), $sContent, '', 3);
                    break;
                } else {
                    $sContent = '你被' . $aClass['classname'] . '解除老师的职位';
                    $this->jpushapi(array($iUid), $sContent, '', 3);
                }
                break;
            default :
                if ($iNpower = 0) {
                    $sContent = '你已被班主任移出' . $aClass['classname'];
                    $this->jpushapi(array($iUid), $sContent, '', 3);
                } else {
                    $sContent = '你被' . $aClass['classname'] . '任命为老师';
                    $this->jpushapi(array($iUid), $sContent, '', 3);
                }
                break;
        }
    }

}
